<template>
	<view class="pageBox">
		<!-- 对于mescroll-body: 需设置:sticky="true", 此应避免在mescroll-body标签前面加其他非定位的元素, 否则下拉区域会被挤出, 无法会隐藏.-->
		<!-- 对于mescroll-uni: 则无需设置:sticky="true", 无其他限制和要求 -->
		<mescroll-body :sticky="true" ref="mescrollRef" :down="downOption" :top="mescroll_top" @init="mescrollInit"
			@down="downCallback" @up="upCallback" :up="upOption">
			<view class="orderContentBox">
				<view class="orderCardBox" v-for="(item,index) in dataList.rows" :key="index">
					<view class="titleBox">
						<view class="title u-line-1">{{item.integralCommodity.businessName}}</view>
						<view class="orderStatus">{{dicts.indent_status[item.indentStatus]}}</view>
					</view>
					<view class="orderCardInfoBox">
						<image class="orderImg" :src="item.integralCommodity.commodityFile" mode="aspectFit"></image>
						<view class="orderCardInfoBox-right">
							<view class="orderTitle u-line-2">{{item.integralCommodity.commodityName}}</view>
							<view class="orderPrice">{{item.integralCommodity.integral}}积分</view>
						</view>
					</view>
					<!-- <view class="orderCardBottom">
						<view class="btn">查看物流</view>
						<view class="btnX"></view>
						<view class="btn1">确认收货</view>
					</view> -->
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js'
	import * as systemApi from "@/api/system.js"
	import * as vouchersApi from "@/api/vouchers.js"
	export default {
		mixins: [MescrollMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		// components: {
		// 	searchRow
		// },
		data() {
			return {
				mescroll_top: '0rpx',
				searchUrl: 'pages/search/afterSales_Search',
				tabsStyle: {
					height: '88rpx',
					boxSizing: 'border-box'
				},
				tabsAStyle: {
					color: '#0138D2'
				},
				tabs: [],
				tabIndex: 0,
				tabIndexOld: 0,
				options: {},
				downOption: {
					auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					auto: false, // 不自动加载
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 8 // 每页数据的数量
					},
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					textNoMore: '--这里是底线--',
					empty: {
						tip: '暂无' // 提示
					}
				},
				dataList: {
					records: [],
					rows: [],
					pages: 0,
					size: 0,
					total: 0,
					current: 0
				}, //列表数据
				listObj: {
					indentStatus: '',
				},
				canReset: false,
				dicts: {
					indent_status: {}
				},
				dictList: {
					indent_status: []
				},
				userInfo: {
					userId: ''
				},
				businessInfo: {
					businessId: '',
					integralBusiness: {
						businessName: ''
					}
				}
			}
		},
		mounted() {
			const that = this
			// // #ifdef MP
			// uni.getSystemInfo({
			// 	success: function(res) {
			// 		that.mescroll_top = 88 + 88 + res.statusBarHeight * 2 + 'rpx'
			// 	}
			// })
			// // #endif
			if (this.$store.getters.platform === 'H5-WEIXIN') {
				that.mescroll_top = 0 + 'rpx'
			} else {
				uni.getSystemInfo({
					success: function(res) {
						that.mescroll_top = 0 + res.statusBarHeight * 2 + 'rpx'
					}
				})
			}
		},
		async onLoad(options) {
			this.options = options
			this.tabs = [{
				label: '全部',
				value: ''
			}]
			this.getDictDataList('indent_status').then((res) => {
				// if (uni.getStorageSync('orderPageM_Search')) {
				// 	const orderPageM_Search = JSON.parse(uni.getStorageSync('orderPageM_Search'))
				// 	this.listObj.indentStatus = orderPageM_Search.indentStatus
				// } else {
				this.listObj.indentStatus = res.rows[0].dictValue
				this.tabs = this.tabs.concat(res.keyValues)
				// }
				// this.tabIndex = this.getTabsIndex(orderPageM_Search.indentStatus)
			})

			this.businessInfo = await this.$store.getters.businessInfo
			if (!this.businessInfo.businessId) {
				await this.$store.dispatch('userBaseInfo').then(res => {
					this.businessInfo = res.businessInfo
				})
			}
			this.downCallback()
		},
		onShow() {
			// that.canReset && that.getData()
			// that.canReset = true
		},
		methods: {
			getDictDataList(dictType) {
				return new Promise((resolve, reject) => {
					systemApi.dictDataList(dictType).then(({
						rows
					}) => {
						let objs = {}
						let keyValues = []
						rows.forEach((item) => {
							objs[item.dictValue] = item.dictLabel
							keyValues.push({
								label: item.dictLabel,
								text: item.dictLabel,
								value: item.dictValue
							})
						})
						this.dicts[dictType] = objs
						this.dictList[dictType] = keyValues
						resolve({
							objs,
							keyValues,
							rows
						})
					}).catch(reject)
				})
			},
			goto(url, data) {
				this.$navTo(`pages/${url}`, data)
			},
			//手动刷新
			getData() {
				let curMescroll = this.$refs.mescrollItem[0].mescroll
				curMescroll && curMescroll.resetUpScroll()
				curMescroll && curMescroll.scrollTo(0, 0)
			},
			getTabsIndex(value) {
				const index = this.tabs.findIndex((item) => item.value == value)
			},
			tabsChange(e) {
				this.tabIndexOld = this.tabIndex
				this.tabIndex = e.index
				this.listObj.indentStatus = e.value
				uni.setStorageSync('orderPageM_Search', JSON.stringify({
					indentStatus: this.listObj.indentStatus,
				}))
				this.downCallback()
			},
			/*下拉刷新的回调 */
			downCallback(page) {
				// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
				// loadSwiper();
				// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				//联网加载数据
				const that = this
				if (that.$store.getters.token) {

					this.getDataList({
						pageNum: page.num,
						pageSize: page.size,
						businessId: this.businessInfo.businessId,
						...that.listObj
					}).then(list => {
						const curPageLen = list.rows.length
						const totalSize = list.total
						that.mescroll.endBySize(curPageLen, totalSize)
					}).catch(err => {
						that.mescroll.endErr()
					})
				} else {
					that.mescroll.endBySize(0, 0)
					return
				}
			},
			/**
			 * @param {Object} page 页码
			 */
			getDataList(pageObj) {
				console.log(pageObj);
				const that = this
				return new Promise((resolve, reject) => {
					vouchersApi.vouchersList(pageObj).then((data) => {
						let dataList = JSON.parse(JSON.stringify(that.dataList))
						dataList.pageSize = pageObj.pageSize
						dataList.pageNum = pageObj.pageNum
						dataList.total = data.total
						dataList.pages = Math.ceil(data.total / dataList.pageSize)
						// 合并新数据
						const newList = data.rows
						// 如果是第一页需手动制空列表
						if (pageObj.pageNum == 1) dataList.rows = []
						// 合并新数据
						dataList.rows = dataList.rows.concat(newList)
						that.dataList = dataList
						resolve(that.dataList)
					}).catch(reject)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	$nav-height: 30px;

	.pageBox {
		.tabsBG {
			padding-left: 30rpx;
			background-color: #F4F7FF;
		}

		background-color: #F4F7FF;

		.orderContentBox {
			padding: 30rpx;
		}

		.orderCardBox {
			// height: 364rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			margin-bottom: 30rpx;
			padding: 30rpx;

			.titleBox {
				margin-bottom: 24rpx;
				display: flex;
				justify-content: space-between;

				.title {
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #000000;
				}

				.orderStatus {
					margin-left: 20rpx;
					flex-shrink: 0;
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
				}
			}

			.orderCardInfoBox {
				display: flex;
				align-items: flex-start;

				.orderImg {
					width: 140rpx;
					height: 140rpx;
					object-fit: cover;
					flex-shrink: 0;
					margin-right: 26rpx;
				}

				.orderCardInfoBox-right {

					.orderTitle {
						font-size: 32rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						color: #000000;
						margin-bottom: 20rpx;
					}

					.orderPrice {
						font-size: 24rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						color: #F00000;
					}
				}
			}

			.orderCardBottom {
				display: flex;
				align-items: flex-start;
				justify-content: flex-end;

				.btn {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #0138D2;
					width: 180rpx;
					height: 76rpx;
					background-color: #fff;
					border-radius: 40rpx;
					border: 2rpx solid #0138D2;
				}

				.btnX {
					width: 30rpx;
				}

				.btn1 {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					width: 180rpx;
					height: 76rpx;
					background: linear-gradient(90deg, #5F96E8 0%, #1977E1 100%);
					border-radius: 40rpx;
				}
			}
		}
	}
</style>